<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 配置
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;


class RewardTask extends Controller
{

    public function index()
    {
        $where = [];
        if (input('type')) {
            if (input('type') != 10) {
                $where['a.type'] = input('type');
            }
        } else {
            $where['a.type'] = 3;
        }

        if (input('status')) {
            if (input('status') != 10) {
                $where['b.status'] = input('status');
            }
        } else {
            $where['b.status'] = 2;
        }

        if (input('first_id')) {
            $where['b.first_id'] = input('first_id');
        }
        if (input('task_id')) {
            $where['b.task_id'] = input('task_id');
        }
        $order = 'id desc';
        if (input('_order') == "task_id") {
            $order = 'b.' . input('_order') . ' ' . (input('_sort') == 'asc' ? 'desc' : 'asc');
        }
        if (input('_order') == "add_time") {
            $order = 'b.' . input('_order') . ' ' . (input('_sort') == 'asc' ? 'desc' : 'asc');
        }
        $list = Db::name('task_release')->alias('a')
            ->join('tp_task_receive b', 'a.id = b.task_id', 'left')
            ->join('tp_user c', 'b.uid = c.id', 'left')
            ->field('b.id,b.title,b.status,b.add_time,b.unit_price,c.nickname,b.task_id,a.case_img,b.img')
            ->where($where)
            ->order($order)
            ->paginate(10, false, ['query' => $this->request->get()]);
        $firstClass = Db::name('task_first_classfy')->select();
        return view('reward_task/index', [
            'list' => $list,
            'firstClass' => $firstClass,
            'page' => $list->render(),
            'count' => $list->total()
        ]);
    }

    //批量审核
    public function flagqmy()
    {
        $ids = input('post.');
        $infos = Db::name('task_receive')->where(['id' => ['in', $ids['ids']], 'status' => 2])->select();
        foreach ($infos as $k => $val) {
            $taskReceive = Db::name('task_receive')->where(['uid' => $val['uid'], 'status' => 3])->select();
            if (count($taskReceive) == 1) {
                //新手任务----完成一次悬赏任务
                $signin = dailyTasks(config('urls') . '/account/addaccount', '完成一次悬赏任务', $val['uid'], 20);
                if (!$signin) {
                    return outputJson(100, 'fail');
                }
            }
            $info = [
                'status' => 3,
                'audit_time' => date('Y-m-d H:i:s')
            ];
            Db::name('task_receive')->where(['id' => $val['id']])->update($info);
            updateredis(config('xsurls') . '/job/updateredis', $val['uid']);
        }
        return ajax_return_adv('1000');
    }

    //全部通过
    public function allauthd()
    {
        $ids = input('post.');
        if ($ids['first_id']) {
            $where['a.first_id'] = $ids['first_id'];
        }
        $where['b.status'] = 2;
        $infos = Db::name('task_release')->alias('a')
            ->join('tp_task_receive b', 'a.id = b.task_id', 'left')
            ->field('b.id,b.uid')->where($where)->select();
        foreach ($infos as $k => $val) {
            $taskReceive = Db::name('task_receive')->where(['uid' => $val['uid'], 'status' => 3])->select();
            if (count($taskReceive) == 1) {
                //新手任务----完成一次悬赏任务
                $signin = dailyTasks(config('urls') . '/account/addaccount', '完成一次悬赏任务', $val['uid'], 20);
                if (!$signin) {
                    return outputJson(1000, 'fail');
                }
            }
            $info = [
                'status' => 3,
                'audit_time' => date('Y-m-d H:i:s')
            ];
            Db::name('task_receive')->where(['id' => $val['id']])->update($info);
            updateredis(config('xsurls') . '/job/updateredis', $val['uid']);
        }
        return ajax_return_adv(1000);
    }

    //审核
    public function edit()
    {
        if ($this->request->post()) {
            $data = input('post.');
            $info = [
                'status' => ($data['type'] == 1) ? 3 : 4
            ];
            $infos = Db::name('task_receive')->where(['id' => $data['id']])->find();
            if ($data['type'] == 1) {

//                $res = addaccountTask(config('urls') . '/account/addaccount', $infos['uid'], ($infos['unit_price'] * 100), $infos['id']);
//                if (!$res) {
//                    return ajax_return_error('失败');
//                }
                $taskReceive = Db::name('task_receive')->where(['uid' => $infos['uid'], 'status' => 3])->select();
                if (count($taskReceive) == 1) {
                    //新手任务----完成一次悬赏任务
                    $signin = dailyTasks(config('urls') . '/account/addaccount', '完成一次悬赏任务', $infos['uid'], 20);
                    if (!$signin) {
                        return outputJson(100, 'fail');
                    }
                }
            } else {
                $info['audit_refuse_reason'] = $data['audit_refuse_reason'];
                $info['audit_refuse_time'] = date('Y-m-d H:i:s');
            }
            $result = Db::name('task_receive')->where(['id' => $data['id']])->update($info);

            if ($result === false) {
                return ajax_return_error('失败');
            }
            updateredis(config('xsurls') . '/job/updateredis', $infos['uid']);
            return ajax_return_adv('成功');
        } else {
            $info = Db::name('task_release')->alias('a')
                ->join('tp_task_receive b', 'a.id = b.task_id', 'left')
                ->join('tp_user c', 'b.uid = c.id', 'left')
                ->field('b.id,b.title,b.add_time,b.case_img,b.unit_price,b.img,b.unit_price,c.nickname')
                ->where(['b.id' => input('id')])
                ->find();
            $info['case_img'] = json_decode(json_decode($info['case_img']));
            $info['img'] = json_decode(json_decode($info['img']));
            $res = Db::name('config')->where(['type' => 10])->find();
            return view('reward_task/edit', ['info' => $info, 'texts' => object_arrays(json_decode($res['texts']))]);
        }
    }

    //审su
    public function appeal()
    {
        if ($this->request->post()) {
            $data = input('post.');
            $info = [
                'status' => ($data['type'] == 1) ? 3 : 8
            ];
            $infos = Db::name('task_receive')->where(['id' => $data['id']])->find();
            if ($data['type'] == 1) {
//                $res = addaccountTask(config('urls') . '/account/addaccount', $infos['uid'], ($infos['unit_price'] * 100), $infos['id']);
//                if (!$res) {
//                    return ajax_return_error('失败');
//                }
                $taskReceive = Db::name('task_receive')->where(['uid' => $infos['uid'], 'status' => 3])->select();
                if (count($taskReceive) == 1) {
                    //新手任务----完成一次悬赏任务
                    $signin = dailyTasks(config('urls') . '/account/addaccount', '完成一次悬赏任务', $infos['uid'], 20);
                    if (!$signin) {
                        return outputJson(100, 'fail');
                    }
                }

            } else {
                $info['appeal_refuse_reason'] = $data['appeal_refuse_reason'];
                $info['appeal_refuse_time'] = date('Y-m-d H:i:s');
            }
            $result = Db::name('task_receive')->where(['id' => $data['id']])->update($info);
            if ($result === false) {
                return ajax_return_error('失败');
            }
            updateredis(config('xsurls') . '/job/updateredis', $infos['uid']);
            return ajax_return_adv('成功');
        } else {
            $info = Db::name('task_release')->alias('a')
                ->join('tp_task_receive b', 'a.id = b.task_id', 'left')
                ->join('tp_user c', 'b.uid = c.id', 'left')
                ->field('b.id,b.title,b.add_time,b.case_img,b.unit_price,b.img,b.unit_price,c.nickname,b.audit_refuse_reason,b.audit_refuse_time,appeal_reason,appeal_reason_time')
                ->where(['b.id' => input('id')])
                ->find();
            $info['case_img'] = json_decode(json_decode($info['case_img']));
            $info['img'] = json_decode(json_decode($info['img']));
            $res = Db::name('config')->where(['type' => 11])->find();
            return view('reward_task/appeal', ['info' => $info, 'texts' => object_arrays(json_decode($res['texts']))]);
        }
    }
}